package com.example.studykt.ui


import android.os.Bundle
import android.os.Environment
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.widget.CompoundButton
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.example.studykt.R
import com.example.studykt.adapter.SmsAdapter
import com.example.studykt.entity.Databus
import com.example.studykt.util.RefreshUtils
import kotlinx.android.synthetic.main.activity_second.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.*


class SecondActivity : AppCompatActivity() {

    var databuses = ArrayList<Databus>()

    lateinit var smsAdapter: SmsAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        EventBus.getDefault().register(this)
        init()

        indata()
    }

    private fun indata() {

        switch_aa.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->

            if (isChecked)
                Toast.makeText(applicationContext, "true", Toast.LENGTH_LONG).show()
            else
                Toast.makeText(applicationContext, "false", Toast.LENGTH_LONG).show()

        })

        val test = { a: Int, b: Int -> a + b }
        val tst1: (Int, Int) -> Int = { a, b -> a + b }

        val arr = "www.chiclaim.com".split(".")
        text.setText(parsePath("/Users/chiclaim/kotlin-book/kotlin-in-action.doc"))


        InputLayout.setOnClickListener {
            edit_Layout.error="layout测试点击显示错误"

        }

        InputEditText.setOnClickListener {
            edit_Layout.isPasswordVisibilityToggleEnabled=false
            edit_EditText.setError("EditText点击显示错误")

        }

        edit_EditText.addTextChangedListener(object :TextWatcher
        {
            var text:String=""
            override fun afterTextChanged(s: Editable?) {
                Log.e("dddddd",s.toString())
                if (!s.toString().equals(text))
                edit_Layout.isPasswordVisibilityToggleEnabled=true
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
                text=s.toString()
                Log.e("dddddd",text)
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

        })

    }

    fun parsePath(path: String): String {
        val directory = path.substringBeforeLast("/")
        val fullName = path.substringAfterLast("/")
        val fileName = fullName.substringBeforeLast(".")
        val extension = fullName.substringAfterLast(".")

        return "Dir: $directory, name: $fileName, ext: $extension,fullname:$fullName"
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    fun Jump(jump: String) {
        Toast.makeText(this, jump, Toast.LENGTH_SHORT).show()
    }


    fun init() {
        RefreshUtils.initList(
            this, Rv_sms_two, RecyclerView.VERTICAL, 2,
            R.color.colorPrimary
        )
        databuses = intent.getParcelableArrayListExtra("fisrt")
        smsAdapter = SmsAdapter()
        Rv_sms_two.adapter = smsAdapter
        smsAdapter.setNewData(databuses)

    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}
